//
//  WBStatusPictureView.swift
//  仿写微博
//
//  Created by tuimao on 17/7/26.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

class WBStatusPictureView: UIView {
    @IBOutlet weak var heightCons:NSLayoutConstraint?
    
    //视图模型
    var viewModel:WBStatusViewModel?{
        didSet{
            /// 根据模型层的配图视图大小。调整单张配图显示内容
            calcViewSize()
            /// 传递图片的url
            urls = viewModel?.picURLs
        }
    }
    /// 根据模型层的配图视图大小。调整单张配图显示内容
    private func calcViewSize(){
        if viewModel?.picURLs?.count == 1 {
            let viewSize = viewModel?.pictureViewSize ?? CGSize()
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: WBStatusPictureOutterMargin, width: viewSize.width, height: viewSize.height - WBStatusPictureOutterMargin)
        }else{
            let v = subviews[0]
            v.frame = CGRect(x: 0, y: WBStatusPictureOutterMargin, width: WBStatusPictureItemWidth, height: WBStatusPictureItemWidth)
        }
        //PictureView的高度
        heightCons?.constant = viewModel?.pictureViewSize.height ?? 0
    }
    
    /// 传递图片的url
    var urls :[WBStatusPicture]?{
        didSet{
         //隐藏所有的视图
            for v in subviews{
                v.isHidden = true
            }
            //遍历数组赋值,当4张图的时候
            var index = 0
            for url in urls ?? [] {
             let iv = subviews[index] as! UIImageView
             if index == 1 && urls?.count == 4 {
                    index += 1
                }
            //转换高清图片
            let bmiddleUrl = url.thumbnail_pic?.replacingOccurrences(of: "thumbnail", with: "bmiddle")
            iv.cz_setImage(urlString: bmiddleUrl, placeholderImage: nil, isAvatar: false)
            iv.isHidden = false
            index += 1
            }
        }
    }
    
    override func awakeFromNib() {
        setupUI()
    }

}


// MARK: - 设置界面
extension WBStatusPictureView {
    
    // 1. Cell 中所有的控件都是提前准备好
    // 2. 设置的时候，根据数据决定是否显示
    // 3. 不要动态创建控件
    fileprivate func setupUI() {
        
        // 设置背景颜色
        backgroundColor = superview?.backgroundColor
        
        // 超出边界的内容不显示
        clipsToBounds = true
        
        let count = 3
        let rect = CGRect(x: 0,
                          y: WBStatusPictureOutterMargin,
                          width: WBStatusPictureItemWidth,
                          height: WBStatusPictureItemWidth)
        
        // 循环创建 9 个 imageView
        for i in 0..<count * count {
            
            let iv = UIImageView()
            // 设置 contentMode
            iv.contentMode = .scaleAspectFill
            iv.clipsToBounds = true
            
            // 行 -> Y
            let row = CGFloat(i / count)
            // 列 -> X
            let col = CGFloat(i % count)
            
            let xOffset = col * (WBStatusPictureItemWidth + WBStatusPictureInnerMargin)
            let yOffset = row * (WBStatusPictureItemWidth + WBStatusPictureInnerMargin)
            
            iv.frame = rect.offsetBy(dx: xOffset, dy: yOffset)
            
            addSubview(iv)
        }
    }
}
